package com.like.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.like.entity.InfoListResp;
import com.like.hospitalworld.R;
import com.like.utils.DateConverter;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;

public class ArticleAdapter extends BaseAdapter {

    public static final int TYPE_PAGER = 0;
    public static final int TYPE_ARTICLE = 1;
    public static final int TYPE_HOT = 2;

    private List<InfoListResp.InformationBase> mInfos;
    private Context mContext;
    private SparseArray<ImgPagerAdapter> mImgsAdapters = new SparseArray<>();
    private SparseArray<List<View>> mAllDots = new SparseArray<>();
    private FragmentManager mFm;
    private boolean mIsIndex = false;

    public ArticleAdapter(FragmentManager fm, Context context, List<InfoListResp.InformationBase> infos, boolean isIndex) {
        this.mFm = fm;
        this.mContext = context;
        this.mIsIndex = isIndex;
        this.mInfos = infos;
        if (infos == null)
            this.mInfos = new ArrayList<>();
    }

    public void update(List<InfoListResp.InformationBase> infos, boolean append) {
        if (!append)
            this.mInfos = infos;
        else
            this.mInfos.addAll(infos);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (!mIsIndex)
            return mInfos.size() + 1;
        else
            return mInfos.size() + 2;
    }

    @Override
    public Object getItem(int position) {
        if (!mIsIndex) {
            if (position == 0)
                return null;
            else
                return mInfos.get(position - 1);
        } else {
            if (position == 0 || position == 1)
                return null;
            else
                return mInfos.get(position - 2);
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (!mIsIndex) {
            if (position == 0)
                return TYPE_PAGER;
            else
                return TYPE_ARTICLE;
        } else {
            if (position == 0)
                return TYPE_PAGER;
            else if (position == 1)
                return TYPE_HOT;
            else
                return TYPE_ARTICLE;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ArticleViewHolder avh = null;
        PagerViewHolder pvh = null;
        HotViewHolder hvh = null;
        Object obj = getItem(position);
        int contentType = getItemViewType(position);
        if (convertView == null) {
            switch (contentType) {
                case TYPE_PAGER:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_pager, parent, false);
                    pvh = new PagerViewHolder(convertView);
                    convertView.setTag(pvh);
                    break;
                case TYPE_ARTICLE:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_article, parent, false);
                    avh = new ArticleViewHolder(convertView);
                    convertView.setTag(avh);
                    break;
                case TYPE_HOT:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hot_read, parent, false);
                    hvh = new HotViewHolder(convertView);
                    convertView.setTag(hvh);
                    break;
            }
        } else {
            switch (contentType) {
                case TYPE_PAGER:
                    pvh = (PagerViewHolder) convertView.getTag();
                    break;
                case TYPE_ARTICLE:
                    avh = (ArticleViewHolder) convertView.getTag();
                    break;
                case TYPE_HOT:
                    hvh = (HotViewHolder) convertView.getTag();
                    break;
            }
        }
        switch (contentType) {
            case TYPE_PAGER:
                if (mImgsAdapters.get(position) == null) {
                    ImgPagerAdapter imgAdapter = new ImgPagerAdapter(mFm);
                    mImgsAdapters.put(position, imgAdapter);
                    pvh.sPager.setAdapter(imgAdapter);
                    pvh.sPager.setCurrentItem(Integer.MAX_VALUE / 2);
                    initDots(position, pvh);
                    setPagerListener(position, pvh.sPager);
                }
                break;
            case TYPE_ARTICLE:
                InfoListResp.InformationBase info = (InfoListResp.InformationBase) obj;
                if (position % 2 == 0)
                    avh.sRootContainer.setBackgroundResource(R.drawable.item_blue_bg);
                else
                    avh.sRootContainer.setBackgroundColor(0xffffff);
                avh.sTitle.setText(info.titleName);
                if(TextUtils.isEmpty(info.summary) || TextUtils.equals("null", info.summary))
                    info.summary = "";
                avh.sContent.setText(info.summary);
                avh.sDate.setText(DateConverter.convertShitToDate(info.putTime));
                break;
            case TYPE_HOT:
                break;
        }
        return convertView;
    }

    private void initDots(int position, PagerViewHolder pvh) {
        List<View> dots = null;
        if (mAllDots.get(position) == null)
            mAllDots.put(position, new ArrayList<View>());
        dots = mAllDots.get(position);
        dots.clear();
        for (int i = 0; i < mImgsAdapters.get(position).getRealCount(); i++) {
            View v = new View(mContext);
            v.setBackgroundResource(R.drawable.index_dot);
            dots.add(v);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(17, 17);
            params.setMargins(0, 0, 10, 0);
            pvh.sDotContainer.addView(v, params);
        }
        changeDotPos(position, 0);
    }

    private void changeDotPos(int position, int dotPosition) {
        List<View> dots = mAllDots.get(position);
        for (int i = 0; i < dots.size(); i++)
            dots.get(i).setSelected(false);
        dots.get(dotPosition).setSelected(true);
    }

    private void setPagerListener(final int itemPosition, ViewPager pager) {
        final ImgPagerAdapter adapter = mImgsAdapters.get(itemPosition);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                changeDotPos(itemPosition, position % adapter.getRealCount());
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    public class ArticleViewHolder {

        @ViewInject(R.id.root_container)
        public ViewGroup sRootContainer;
        @ViewInject(R.id.title)
        public TextView sTitle;
        @ViewInject(R.id.content)
        public TextView sContent;
        @ViewInject(R.id.date)
        public TextView sDate;

        public ArticleViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

    }

    public class PagerViewHolder {

        @ViewInject(R.id.top_pager)
        public AutoScrollViewPager sPager;
        @ViewInject(R.id.dot_container)
        public ViewGroup sDotContainer;

        public void setTags(int position) {
            sPager.setTag(position);
        }

        public PagerViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

    }

    public class HotViewHolder {

        public HotViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

    }
}
